const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,
    name: '',
    activityList: [], //学术活动
    itemsList: [],   //品牌项目
    coursesList: [], // 学院课程
    academics: [],   // 学术频道
    page: 1,         // 当前页数
    pagesize: 10     // 每页条数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.showLoading();
    const name = options.name;
    this.setData({
      name
    })
   
  },
  /**
* 生命周期函数--监听页面显示
*/
  onShow: function () {
    const name = this.data.name;
    name == 'activities' && this.getActivityList();
    name == 'items' && this.getItemsList();
    name == 'courses' && this.getCoursesList();
    name == 'academics' && this.getAcademicList();
  },

  //学术活动
  getActivityList() {
    const that = this;
    const param = {
      page: that.data.page,
      pagesize: that.data.pagesize
    };

    app.request({
      url: 'train/activity/getActivityList',
      data: param,
      success: function(res) {
        let activityList = res.data;
        if (param.page != 1) {
          activityList = that.data.activityList.concat(activityList);
        }
        that.setData({
          activityList
        })
        wx.hideLoading();
      },
      fail: function(res) {
        
      }
    });
  },
  //品牌项目
  getItemsList() {
    const that = this;
    const param = {
      page: that.data.page,
      pagesize: that.data.pagesize
    };

    app.request({
      url: 'train/item/getBrandItemList',
      data: param,
      success: function(res) {
        let itemsList = res.data;
        //判断页数
        if (param.page != 1) {
          itemsList = that.data.itemsList.concat(itemsList);
        }
        that.setData({
          itemsList
        })
        wx.hideLoading();
      },
      fail: function(res) {
        
      }
    });
  },
  //学院课程
  getCoursesList() {
    const that = this;
    const param = {
      page: that.data.page,
      pagesize: that.data.pagesize
    };
    app.request({
      url: 'train/course/getCourseList',
      data: param,
      success: function(res) {
        let coursesList = res.data;
        //判断页数
        if (param.page != 1) {
          coursesList = that.data.coursesList.concat(coursesList);
        }
        that.setData({
          coursesList
        })
        wx.hideLoading();
      },
      fail: function(res) {
        
      }
    });
  },
  //学术频道
  getAcademicList() {
    const that = this;
    const param = {
      page: that.data.page,
      pagesize: that.data.pagesize
    };
    app.request({
      url: 'train/academic/getAcademicList',
      data: param,
      success: function(res) {
        let academics = res.data;
        if (param.page != 1) {
          academics = that.data.academics.concat(academics);
        }
        that.setData({
          academics
        })
        wx.hideLoading();
      },
      fail: function(res) {
        
      }
    });
  },

  //进入详情
  entry(e) {
    const id = e.currentTarget.dataset.id;
    const classify = e.currentTarget.dataset.classify;
    const obj = {
      id,
      classify
    }
    wx.navigateTo({
      url: '/pages/doctor-train-details/doctor-train-details?obj=' + JSON.stringify(obj)
    });
  },

  //加载更多
  moreInfo() {
    let page = this.data.page + 1;
    this.setData({
      page
    });
    this.data.name == 'activities' && this.getActivityList();
    this.data.name == 'items' && this.getItemsList();
    this.data.name == 'academics' && this.getAcademicList();
    this.data.name == 'courses' && this.getCoursesList();
  },
  // 刷新
  upper() {
    const name = this.data.name;
    name == 'activities' && this.getActivityList();
    name == 'items' && this.getItemsList();
    name == 'courses' && this.getCoursesList();
    name == 'academics' && this.getAcademicList();
  }
})